********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*****
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(2) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**#Bookmark (input)
clear *
import sas using $inputdirectory/bloansmonpol, case(lower)
keep if disbur>=30000|com>=30000
drop com

format borrowerid %20.0g
format disbur %20.0g

tostring(time), replace
gen xxx=date(time, "YMD")
drop time
gen time=mofd(xxx)
drop xxx
format time %tm
gen xxx=dofm(time)
gen year=year(xxx)
drop xxx

**#Bookmark (input)
merge m:1 bankid year using $inputdirectory/INB_typesmonpol, keepus(type)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid year using $inputdirectory/INB_grumonpol, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.

gen xxx=type if bankid==gru
egen grutype=max(xxx), by(gru)
replace grutype=10 if grutype==.
drop xxx
keep if grutype==10

drop bankid
rename gru bankid

collapse (sum) disbur (max) year, by(bankid borrowerid time)

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2010m7)&time<=tm(2020m2)
drop if time==tm(2018m1)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)
egen nrel=count(bankid), by(borrowerid time)
keep if nrel>=2
drop nrel lncdisbur

**#Bookmark (input)
merge m:1 time using $inputdirectory/INB_shocksmonpol
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_contr&expmonpol
keep if _merge!=2
drop _merge

gen IT10Ylagmma=IT10Y*lagmma
gen IT2Ylagmmam5=IT2Y*lagmmam5
gen IT10Ylagbook3=IT10Y*lagbook3
gen IT10Ylagbook1=IT10Y*lagbook1
gen IT10Ylagafs=IT10Y*lagafs
gen IT10Ylagafsm5=IT10Y*lagafsm5

**#Bookmark (input)
merge m:1 bankid year using $inputdirectory/INB_capitalmonpol
keep if _merge==3

gen count=1
sort bankid year
by bankid year: gen uniquebankid=sum(count)
sum ourt1r if uniquebankid==1, d
scalar define s1=r(p25)
gen ourt1rold=ourt1r
replace ourt1r=ourt1r-scalar(s1)
gen IT10Ylagmmaourt1r=IT10Ylagmma*ourt1r
gen IT10Yourt1r=IT10Y*ourt1r
gen lagmmaourt1r=lagmma*ourt1r
gen IT10Ylagcashas=IT10Y*lagcashas
gen lagmmalagcashas=lagmma*lagcashas
gen IT10Ylagmmalagcashas=IT10Ylagmma*lagcashas
gen IT10Ylagmmaourt1rlagcashas=IT10Ylagmmaourt1r*lagcashas
gen IT10Yourt1rlagcashas=IT10Yourt1r*lagcashas
gen lagmmaourt1rlagcashas=lagmmaourt1r*lagcashas
gen ourt1rlagcashas=ourt1r*lagcashas
drop count uniquebankid 
compress

**#Bookmark (output - column 5-1-2-3-4)
cd $outputdirectory
est clear
reghdfe $y IT10Ylagmma lagmma IT10Ylagmmaourt1r IT10Yourt1r lagmmaourt1r ourt1r IT10Ylagmmalagcashas IT10Ylagcashas lagmmalagcashas lagcashas IT10Ylagmmaourt1rlagcashas IT10Yourt1rlagcashas lagmmaourt1rlagcashas ourt1rlagcashas laglnassets lncrefin $regcommands
keep if e(sample)==1
est save TAB9_C51234
reghdfe $y IT10Ylagmma lagmma laglnassets lagcashas lncrefin $regcommands
est save TAB9_C51234, append
reghdfe $y IT10Ylagmma lagmma IT2Ylagmmam5 lagmmam5 laglnassets lagcashas lncrefin $regcommands
est save TAB9_C51234, append
reghdfe $y IT10Ylagmma lagmma laglnassets lagcashas lncrefin if time!=tm(2015m1)&time!=tm(2019m9) $regcommands
est save TAB9_C51234, append

gen post=(time>tm(2018m1))
gen postIT10Ylagafs=post*IT10Ylagafs
gen postlagafs=post*lagafs
reghdfe $y  IT10Ylagafs postIT10Ylagafs IT10Ylagbook3 IT10Ylagbook1 lagafs lagbook3 lagbook1 postlagafs laglnassets lagcashas lncrefin $regcommands
est save TAB9_C51234, append
cd ..
